package com.lkm.market2_0.m_imple.steelyardstatus;

import com.lkm.frame.P;
import com.lkm.frame.task2.ITaskExecutor;
import com.lkm.frame.task2.Task;
import com.lkm.frame.task2.TaskManager;
import com.lkm.javatool.lang.NameValueP;
import com.lkm.market2_0.ExpandConfig;
import com.lkm.market2_0.entity.ResponEntity;
import com.lkm.market2_0.m.steelyardstatus.StatusInfo;
import com.lkm.market2_0.m.steelyardstatus.SteelyardStatusManagerModule;
import com.lkm.market2_0.netapi.Api;
import com.lkm.market2_0.m_imple.steelyardserver.server.ReceiveTask;
import com.lkm.market2_0.task.AsyncTask2;
import com.lkm.market2_0.task.DefaultTaskExecutor;




 class UpStatusTask extends AsyncTask2<Void, Void, ResponEntity<Void>> {

    SteelyardStatusManagerModule mSteelyardStatusManagerModule;
    public UpStatusTask(ITaskExecutor<Void, Void, ResponEntity<Void>> taskExecutor, TaskManager mTaskManager,SteelyardStatusManagerModule mSteelyardStatusManagerModule) {
        super(taskExecutor, UpStatusTask.class.getName(), mTaskManager);
        this.mSteelyardStatusManagerModule=mSteelyardStatusManagerModule;
    }

    




    public static boolean isUpdateIng() {
        return ExpandConfig.instance.getTaskManager2().getRunTask(UpStatusTask.class.getName()) != null;
    }

    public static UpStatusTask up(SteelyardStatusManagerModule mSteelyardStatusManagerModule) {
        P.p("====批量上传状态");

        int ts[] = ExpandConfig.instance.getTaskManager2().getRunTask(UpStatusTask.class.getName());
        if (ts != null) {
            Task task = ExpandConfig.instance.getTaskManager2().getRunTask(ts[0]);
            task.cancel();
        }
        UpStatusTask upStatusTask = new UpStatusTask(new DefaultTaskExecutor<Void, Void, ResponEntity<Void>>(true), ExpandConfig.instance.getTaskManager2(),mSteelyardStatusManagerModule);
        upStatusTask.action();
        upStatusTask.execTask();
        return upStatusTask;
    }

    @Override
    protected void doAsyncExecuting(Void param) {

        SteelyardStatusManagerModule steelyardStatusManagerModule = mSteelyardStatusManagerModule;

        StatusInfo[] statusInfos = steelyardStatusManagerModule.getStatusInfos();

        NameValueP[] statuss = new NameValueP[statusInfos.length];

        for (int i = 0; i < statusInfos.length; i++) {
            StatusInfo si = statusInfos[i];
            String status = "1";
            
            if (si.isOnline()) {
                status = "1";
            } else {
                status = "4";
            }
            if (si.isException()) {
                status = "3";
            }
            statuss[i] = new NameValueP(si.getSteelyardMac(), status+"-"+si.getVersion());
        }

        if(!Api.isASTApi())
        mTaskExecutor.executor(new Runnable() {
            @Override
            public void run() {
                
                Api.upStatus(true, statuss, UpStatusTask.this);

            }
        });

        ResponEntity<String> re = ResponEntity.fromJson(

                Api.upStatus(false, statuss, UpStatusTask.this)
                , String.class, ""
        );
        if(re!=null&&re.getData()!=null){
            try{
                ExpandConfig.instance.receiverPushContent(re.getData());
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        TaskManager taskManager= ExpandConfig.instance.getTaskManager2();

        
        for (int i = 0; i < statusInfos.length; i++) {
            StatusInfo si = statusInfos[i];

            String taskId=ReceiveTask.createId(si.getSteelyardMac());

            
            int [] runTaskSers=taskManager.getRunTask(taskId);
            if(runTaskSers!=null){
                for (int taskSer:runTaskSers) {
                    ReceiveTask task = (ReceiveTask)taskManager.getRunTask(taskSer);
                    if(task!=null){
                        if(task.getIHardwareControler()!=null&&task.getIHardwareControler().isConnected()){
                            
                            P.p("====ReceiveTask 台秤有连接的"+si.getSteelyardMac());
                        }else {
                            P.p("====ReceiveTask 台秤断开的的"+si.getSteelyardMac());
                            task.cancel();
                        }
                    }else{
                        P.p("====ReceiveTask 台秤断开的的"+si.getSteelyardMac());
                    }
                }
            }

        }

        complete(new ResponEntity<>(re));
    }


}
